[

define string->asInteger => integer(self)
define string->asDecimal => decimal(self)


define string->digits::string => .asString->regex('\\D','')
//'1-555-456-7890'->digits;


define string->encodeurl::string => encode_url(self)

// -------------------------------------------------------------------------------
// string->split(delimiter, maxsplit)
// returns a string split by the delimiter, maxsplit > 0 times from left, or maxsplit < 0 times from right
define string->split(delimiter::string, maxsplit::integer)::array => {

	local(split) = .split(#delimiter)
	
	if(#maxsplit->abs == 1) // split size of 1?
		return(array(self))
	else(#maxsplit == 0 || #maxsplit->abs >= #split->size)
		return(#split)
	else
		local(out=array)
		if(#maxsplit < 0)
			#split->reverse
		/if
		loop(#maxsplit->abs - 1)
			#out->insert(#split->get(1))
			#split->remove(1)
		/loop
		if(#maxsplit < 0)
			#split->reverse
			#out->insert( #split->join(#delimiter))
			#out->reverse
		else
			#out->insert( #split->join(#delimiter))
		/if
		return(#out)
	/if
}

/*
'a#b#c#d#e'->split('#')
'a#b#c#d#e'->split('#',3)
'a#b#c#d#e'->split('#',-3)
*/


// -------------------------------------------------------------------------------
define string->prepend(s::string) => .private_merge(#s,1,0)

define string->removeleading(find::string,want::integer) => {
  (#want == 0 ? .removeleading(#find))
  if(#want > 0)
    .removeleading(#find)
    .prepend(#find * #want)
  else // #want < 0
    local('c') = 0
    local('max') = #want * -1
    local('loop') = (#max > .size ? .size | #max)
    while(++#c <= #loop && .get(1) == #find)
      .remove(1,1)
    /while
  /if
}

define string->removetrailing(find::string,want::integer) => {
  (#want == 0 ? .removetrailing(#find))
  if(#want > 0)
    .removetrailing(#find)
    .append(#find * #want)
  else // #want < 0
    local('c') = 0
    local('max') = #want * -1
    local('loop') = (#max > .size ? .size | #max)
    while(++#c <= #loop && .get(.size) == #find)
      .remove(.size)
    /while
  /if
}

/*
var('test') = 'aaabbb'
$test->removeleading('a',1)
$test->removetrailing('b',1)
$test
*/

define string->regex(find::string, -ignorecase::boolean=false, -matchlimit::integer=100000)::string
	=> string_findregexp(self, -find=#find, -ignorecase=#ignorecase, -matchlimit=#matchlimit)

define string->regex(find::string, replace::string, -ignorecase::boolean=false, -matchlimit::integer=100000, -replaceonlyone::boolean=false, -inplace::boolean=false)::string => { 
	local(s) = string_replaceregexp(self, -find=#find, -replace=#replace, -ignorecase=#ignorecase, -matchlimit=#matchlimit, -replaceonlyone=#replaceonlyone)
	#inplace
		? .remove(1)&append(#s)
		| return #s
}

/*
=========================================================
[
var('a') = 'abcbd';
$a->regex('b');'<br>';
$a->regex('b','*');'<br>';
$a;'<br>';
$a->regex('b','-',-inplace);
$a;
]

L9   =>  array(b, b)
         a*c*d
         abcbd
         a-c-d
=========================================================
*/

// get the linefeed
define string->linefeed(-printable::boolean=false) => {
	if(#printable == true)
		local(lf) = .regex('\r\n|\r|\n')->seek(1,null)
		select(#lf)
			case('\r\n') return '\\r\\n'
			case('\r') return '\\r'
			case('\n') return '\\n'
			case return('')
		/select
	/if;
	return .regex('\r\n|\r|\n')->seek(1,null)
}

// set the linefeed
define string->linefeed(linefeed::string) => .regex('\r\n|\r|\n',#linefeed,-inplace)


/*
'abc\r123'->linefeed(-printable)
'<br>'
'abc\n123'->linefeed(-printable)
'<br>'
'abc\r\n123'->linefeed(-printable)
'<br>'
'abc\r123'->linefeed->encodeurl
'<br>'
'abc\n123'->linefeed->encodeurl
'<br>'
'abc\r\n123'->linefeed->encodeurl
'<br>'
'abc\r123'->linefeed('\n')&encodeurl
'<br>'
'abc\n123'->linefeed('\n')&encodeurl
'<br>'
'abc\r\n123'->linefeed('\n')&encodeurl
'<br>'
*/


define string->encodebase64url::string => self->encodebase64->removetrailing('=')&replace('+','-')&replace('/','_')&;
define string->decodebase64url::bytes => self->decodebase64->replace('-','+')&replace('_','/')&;
/*
var('string') = '!@#$%^&*()1234567890';
$string->encodebase64;
'<br>';
$string->encodebase64url;
*/


define string->find(find::tag,...)::integer => self->find(string(#find))
//'integer' >> integer->type

define ALPHANUMERIC::string => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

define string_random(size::integer=20)::string => {
  local(randomString) = string
  loop(#size)
    #randomString += ALPHANUMERIC->get(math_random(-lower=1,-upper=62))
  /loop
  return #randomString
}
/*
loop(10)
string_random(loop_count) '<br>'
string_random '<br>'
/loop
*/

define string->encodehex => self->asBytes->encodehex
define string->decodehex => self->asBytes->decodehex
// 	'abc'->encodehex
//	'616263'->decodehex

]